package com.ffapp.demo.chat;

import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ffapp.demo.R;
import com.ffapp.demo.basis.BasisActivity;
import com.ffapp.demo.basis.BasisApp;
import com.ffapp.demo.bean.LoginBean;
import com.ffapp.demo.chat.model.ChatMsgListBean;
import com.ffapp.demo.chat.model.ChatMsgListData;
import com.ffapp.demo.map.GPSUtil;
import com.ffapp.demo.map.MapLocationActivity;
import com.ffapp.demo.other.PhotoScanDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AdapterChat extends RecyclerView.Adapter<AdapterChat.ViewHolder> {


        private ChatMsgListBean mListBean;
        BasisActivity mContext;
        private TopMoreClickListener mListener;

        public AdapterChat(BasisActivity context,ChatMsgListBean listBean,TopMoreClickListener listener) {
            mContext = context;
            mListBean = listBean;
            mListener = listener;
        }

        public void setData(ChatMsgListBean listBean){
            this.mListBean = listBean;
            notifyDataSetChanged();
        }

//    public void update(ChatMsgListBean listBean){
//            if(mListBean == null || mListBean.getList().size() == listBean.getList().size()){
//                mListBean = listBean;
//                notifyDataSetChanged();
//            }else {
//                int itemChange = listBean.getList().size() - mListBean.getList().size();
//                notifyItemInserted();
//            }
//    }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_list_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            if(mListBean == null )return 0;

            return mListBean.getList().size() + (mListBean.isHasMoreTop()?1:0);
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_time)
            public TextView text_time;
            @BindView(R.id.view_left_zone)
            public View view_left_zone;
            @BindView(R.id.img_photo_left)
            public ImageView img_photo_left;
            @BindView(R.id.img_img_left)
            public ImageView img_img_left;
            @BindView(R.id.text_content_left)
            public TextView text_content_left;
            @BindView(R.id.text_status_left)
            public TextView text_status_left;
            @BindView(R.id.text_location_title_left)
            public TextView text_location_title_left;
            @BindView(R.id.text_location_des_left)
            public TextView text_location_des_left;
            @BindView(R.id.img_location_left)
            public ImageView img_location_left;
            @BindView(R.id.view_location_left)
            public View view_location_left;

            @BindView(R.id.view_right_zone)
            public View view_right_zone;
            @BindView(R.id.img_photo_right)
            public ImageView img_photo_right;
            @BindView(R.id.img_img_right)
            public ImageView img_img_right;
            @BindView(R.id.text_content_right)
            public TextView text_content_right;
            @BindView(R.id.text_status_right)
            public TextView text_status_right;
            @BindView(R.id.text_name_left)
            public TextView text_name_left;
            @BindView(R.id.text_name_right)
            public TextView text_name_right;
            @BindView(R.id.text_location_title_right)
            public TextView text_location_title_right;
            @BindView(R.id.text_location_des_right)
            public TextView text_location_des_right;
            @BindView(R.id.img_location_right)
            public ImageView img_location_right;
            @BindView(R.id.view_location_right)
            public View view_location_right;



            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                if(mListBean.isHasMoreTop() && position == 0){
                    text_time.setVisibility(View.VISIBLE);
                    text_time.setText("查看更多");
                    img_img_left.setVisibility(View.GONE);
                    view_left_zone.setVisibility(View.INVISIBLE);
                    view_right_zone.setVisibility(View.GONE);
                    text_time.setTag(position);
                    return;
                }
               if(mListBean.isHasMoreTop()){
                   position--;
               }
                ChatMsgListData data = mListBean.getList().get(position);
                ChatMsgListData preData = position == 0?null:mListBean.getList().get(position-1);
                String timeShow = data.getTimeShow_STR(preData);
                if(TextUtils.isEmpty(timeShow)){
                    text_time.setVisibility(View.GONE);
                }else {
                    text_time.setVisibility(View.VISIBLE);
                    text_time.setText(timeShow);
                }

                if(data.getSendUser().equals(LoginBean.getUserId())){
                    view_left_zone.setVisibility(View.GONE);
                    view_right_zone.setVisibility(View.VISIBLE);
                    text_name_right.setText(data.getSendUserName());
                    view_location_right.setVisibility(View.GONE);
                    text_content_right.setVisibility(View.GONE);
                    text_status_right.setVisibility(View.GONE);
                    img_img_right.setVisibility(View.GONE);
                    BasisApp.loadImg(mContext,data.getAvatar(),img_photo_right,R.drawable.chat_icon_avatar);
//                    img_img_right
                    if(data.getType().equals(ChatMsgListData.TYPE_TEXT)){
                        text_content_right.setVisibility(View.VISIBLE);
                        text_content_right.setText(data.getContent());
                    }else if(data.getType().equals(ChatMsgListData.TYPE_USERSTATE)){
                        text_status_right.setVisibility(View.VISIBLE);
                        text_status_right.setText(Html.fromHtml(data.contentJsonToString()));
                    }else if(data.getType().equals(ChatMsgListData.TYPE_IMAGE)){
                        img_img_right.setVisibility(View.VISIBLE);
                        BasisApp.loadImg(mContext,data.getContent(),img_img_right,R.drawable.base_img_default_square);
                    }else if(data.getType().equals(ChatMsgListData.TYPE_LOCATION)){
                        view_location_right.setVisibility(View.VISIBLE);
                        text_location_title_right.setText(data.getPositionName());
                        text_location_des_right.setText(data.getPositionAddress());
                        BasisApp.loadImg(mContext,data.getPositionImageUrl(),img_location_right,R.drawable.base_img_default_square);
                    }
                }else {
                    view_left_zone.setVisibility(View.VISIBLE);
                    view_right_zone.setVisibility(View.GONE);
                    view_location_left.setVisibility(View.GONE);
                    text_content_left.setVisibility(View.GONE);
                    text_status_left.setVisibility(View.GONE);
                    img_img_left.setVisibility(View.GONE);
                    text_name_left.setText(data.getSendUserName());
                    BasisApp.loadImg(mContext,data.getAvatar(),img_photo_left,R.drawable.chat_icon_avatar);
                    if(data.getType().equals(ChatMsgListData.TYPE_TEXT)){
                        text_content_left.setVisibility(View.VISIBLE);
                        text_content_left.setText(data.getContent());
                    }else if(data.getType().equals(ChatMsgListData.TYPE_USERSTATE)){
                        text_status_left.setVisibility(View.VISIBLE);
                        text_status_left.setText(Html.fromHtml(data.contentJsonToString()));
                    }else if(data.getType().equals(ChatMsgListData.TYPE_IMAGE)){
                        img_img_left.setVisibility(View.VISIBLE);
                        BasisApp.loadImg(mContext,data.getContent(),img_img_left,R.drawable.base_img_default_square);
                    }else if(data.getType().equals(ChatMsgListData.TYPE_LOCATION)){
                        view_location_left.setVisibility(View.VISIBLE);
                        text_location_title_left.setText(data.getPositionName());
                        text_location_des_left.setText(data.getPositionAddress());
                        BasisApp.loadImg(mContext,data.getPositionImageUrl(),img_location_left,R.drawable.base_img_default_square);
                    }
                }

                img_img_left.setTag(R.id.img_tag,position);
                img_img_right.setTag(R.id.img_tag,position);
                view_location_left.setTag(position);
                view_location_right.setTag(R.id.img_tag,position);
                list_item.setTag(position);
                text_time.setTag(position);
                view_location_right.setTag(position);
                view_location_left.setTag(position);
            }

            @OnClick({R.id.img_img_left,R.id.img_img_right})
            void onImageClick(View view){
                int position = (int) view.getTag(R.id.img_tag);
                ChatMsgListData data = mListBean.getList().get(position);
                if(data == null)return;
                if(TextUtils.isEmpty(data.getContent()))return;
                PhotoScanDialog.toImgScanDialog(mContext,data.getContent());
            }

            @OnClick(R.id.text_time)
            void onTimeClick(View view){
                if(mListBean == null)return;
                if(!mListBean.isHasMoreTop())return;
                int position = (int) view.getTag();
                if(position != 0)return;
                if(mListener != null){
                    mListener.onTopMoreClick();
                }

            }
            @OnClick({R.id.view_location_right,R.id.view_location_left})
            void onLocationClick(View view){
                if(mListBean == null)return;
                int position = (int) view.getTag();
                ChatMsgListData data = mListBean.getList().get(position);
                if(TextUtils.isEmpty(data.getContent()) || !data.getContent().contains(","))return;
                String[] points = data.getContent().split(",");
                double[] pointsD = GPSUtil.gps84_To_Gcj02(Double.valueOf(points[1]),Double.valueOf(points[0]));
                MapLocationActivity.toMapActivity(mContext,pointsD[0],pointsD[1],data.getPositionName(),data.getPositionAddress());
            }
        }

    public interface TopMoreClickListener {
        abstract void onTopMoreClick();
    }

}